package edu.scut.course_management.DataFill;

import edu.scut.course_management.controller.PersonManagementController;
import edu.scut.course_management.dto.RequestStudentDTO;
import edu.scut.course_management.dto.RequestTeacherDTO;
import edu.scut.course_management.po.Class;
import edu.scut.course_management.result.JsonResult;
import edu.scut.course_management.service.InfomationService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@SpringBootTest
public class DataFill {
    @Autowired
    private PersonManagementController personManagementController;
    @Autowired
    private InfomationService infomationService;

    private int studentNum;

    @Test
    public void addTeacher() {
        List<RequestTeacherDTO> requestTeacherDTOS = generateTeacher();
        for (RequestTeacherDTO requestTeacherDTO: requestTeacherDTOS) {
            personManagementController.addTeacherInfo(requestTeacherDTO);
        }
    }

    private List<RequestTeacherDTO> generateTeacher() {
        List<RequestTeacherDTO> teacherInfos = new ArrayList<>();
        String[] academicTitles = {"副教授", "教授", "研究员", "讲师"};
        String[] phoneNumPrefix = {"131", "158", "136", "159"};
        Boolean[] males = {true, false};
        Random random = new Random();
        for (int i = 0; i < 400; i++) {
            // 生成9位教师ID
            String teacherId = String.valueOf(i);
            StringBuilder stringBuilder = new StringBuilder();
            for (int j = 0; j < 9 - teacherId.length(); j++) {
                stringBuilder.append("0");
            }
            stringBuilder.append(teacherId);
            teacherId =  stringBuilder.toString();
            // 生成教师名字
            String teacherName = "teacher" + String.valueOf(i);
            // 教师职称
            String academicTitle = academicTitles[random.nextInt(4)];
            // 学院
            Integer departmentId = random.nextInt(15) + 1;
            // 手机号
            String phoneNum = phoneNumPrefix[random.nextInt(4)]
                    + String.valueOf(random.nextInt(9000) + 1000)
                    + String.valueOf(random.nextInt(9000) + 1000);
            // 性别
            Boolean male = males[random.nextInt(2)];
            teacherInfos.add(new RequestTeacherDTO(teacherId, teacherName, male, phoneNum, departmentId, academicTitle));
        }
        return teacherInfos;
    }

    @Test
    public void addStudent() {
        // 获得软件学院的班级列表
        String[] enrollmentYears = {"2017", "2018", "2019", "2020"};
        for (int i = 0; i < enrollmentYears.length; i++) {
            List<Class> classes = infomationService.getMajorClasses(1, enrollmentYears[i]);
            for (Class majorClass: classes) {
                addStudent(majorClass);
            }
        }

    }

    private void addStudent(Class majorClass) {
        String[] phoneNumPrefix = {"131", "158", "136", "159"};
        Boolean[] males = {true, false};
        String[] dormitoryBuilding = {"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "C14", "C15"};
        String[] dormitoryRoom = {"101","102","103","104","105", "201","202","203","204","205"};
        Random random = new Random();
        String studentPrefix = "20183067";
        for (int i = 0; i < 50; i++) {
            // 学号
            String studentId = String.valueOf(studentNum);
            StringBuilder stringBuilder = new StringBuilder();
            for (int j = 0; j < 4 - studentId.length(); j++) {
                stringBuilder.append("0");
            }
            stringBuilder.append(studentId);
            studentId = studentPrefix + stringBuilder.toString();
            // 姓名
            String studentName = "student" + String.valueOf(studentNum++);
            // 性别
            Boolean male = males[random.nextInt(2)];
            // 专业id
            Integer majorId = majorClass.getMajorId();
            // 入学年份
            String enrollmentYear = majorClass.getEnrollmentYear();
            // 班级名字
            String className = majorClass.getClassName();
            // 手机号
            String phoneNum = phoneNumPrefix[random.nextInt(4)]
                    + String.valueOf(random.nextInt(9000) + 1000)
                    + String.valueOf(random.nextInt(9000) + 1000);
            // 地址
            String address = dormitoryBuilding[random.nextInt(12)] + "-" + dormitoryRoom[random.nextInt(10)];
            personManagementController.addStudentInfo(new RequestStudentDTO(studentId, studentName, male, address, phoneNum, majorId, enrollmentYear, className, null));
        }
    }


}
